<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>会议查询 - 会议室预订系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 1400px; /* 扩大容器最大宽度 */
            margin: 0 auto;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .container::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            z-index: -1;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .header p {
            color: #666;
            font-size: 16px;
        }

        .search-form {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .form-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
        }

        .form-group label {
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input, .form-group select {
            padding: 12px 15px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus, .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .search-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .search-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 15px rgba(102, 126, 234, 0.3);
        }

        .reset-btn {
            background: #f1f2f6;
            color: #666;
        }

        .reset-btn:hover {
            background: #dfe4ea;
        }

        .results-section {
            margin-top: 30px;
        }

        .section-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .table-container {
            overflow-x: auto;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            background: #4a90e2; /* 更和谐的表头颜色 */
            color: white;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f8f9fa;
        }

        .no-results {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 18px;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 25px;
            border-radius: 8px;
            color: white;
            font-weight: 500;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            z-index: 2000;
            display: none;
            animation: slideIn 0.3s ease;
        }

        .notification.success {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
        }

        .notification.error {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
        }

        @keyframes slideIn {
            from {
                transform: translateX(100%);
                opacity: 0;
            }
            to {
                transform: translateX(0);
                opacity: 1;
            }
        }

        /* 返回链接容器 */
        .back-link-container {
            margin-bottom: 20px;
            position: relative;
            z-index: 1;
        }

        /* 返回链接样式 */
        .back-link {
            display: inline-flex;
            align-items: center;
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            padding: 8px 16px;
            border-radius: 8px;
        }

        .back-link:hover {
            background: rgba(102, 126, 234, 0.1);
            text-decoration: none;
            transform: translateX(-3px);
        }

        /* 分页样式 */
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
            gap: 10px;
        }

        .pagination button {
            padding: 8px 12px;
            background: #f1f2f6;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .pagination button:hover:not(:disabled) {
            background: #4a90e2;
            color: white;
        }

        .pagination button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        .pagination .current-page {
            font-weight: bold;
            color: #4a90e2;
        }

        .pagination-info {
            margin-top: 15px;
            text-align: center;
            color: #666;
            font-size: 14px;
        }

        /* 操作按钮样式 */
        .action-buttons {
            display: flex;
            gap: 5px;
        }

        .action-btn {
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.3s ease;
        }

        .update-btn {
            background: #4a90e2;
            color: white;
        }

        .update-btn:hover {
            background: #357ae8;
        }

        .delete-btn {
            background: #e74c3c;
            color: white;
        }

        .delete-btn:hover {
            background: #c0392b;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 3000;
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            width: 90%;
            max-width: 500px;
            padding: 30px;
            position: relative;
            animation: modalSlideIn 0.3s ease;
        }

        @keyframes modalSlideIn {
            from {
                transform: translateY(-50px);
                opacity: 0;
            }
            to {
                transform: translateY(0);
                opacity: 1;
            }
        }

        .close {
            position: absolute;
            right: 20px;
            top: 20px;
            font-size: 24px;
            font-weight: bold;
            cursor: pointer;
            color: #aaa;
        }

        .close:hover {
            color: #333;
        }

        .modal-header {
            margin-bottom: 20px;
            text-align: center;
        }

        .modal-header h2 {
            color: #333;
            font-size: 24px;
        }

        .modal-body {
            margin-bottom: 20px;
        }

        .form-row {
            margin-bottom: 15px;
        }

        .form-row label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: 500;
        }

        .form-row input, .form-row select, .form-row textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-row input:focus, .form-row select:focus, .form-row textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .modal-footer {
            display: flex;
            justify-content: center;
            gap: 15px;
        }

        .modal-btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .save-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .save-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 15px rgba(102, 126, 234, 0.3);
        }

        .cancel-btn {
            background: #f1f2f6;
            color: #666;
        }

        .cancel-btn:hover {
            background: #dfe4ea;
        }

        @media (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
            
            th, td {
                padding: 10px 8px;
                font-size: 14px;
            }
            
            .container {
                max-width: 100%;
                padding: 15px;
            }
            
            .modal-content {
                width: 95%;
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="back-link-container">
            <a href="/api/major.html" class="back-link">
                <i class="fas fa-arrow-left"></i> 返回主页
            </a>
        </div>
        
        <div class="header">
            <h1><i class="fas fa-search"></i> 会议查询</h1>
            <p>根据条件查询会议预订记录</p>
        </div>
        
        <div class="search-form">
            <h2 class="form-title"><i class="fas fa-filter"></i> 查询条件</h2>
            <form id="searchForm">
                <div class="form-grid">
                    <div class="form-group">
                        <label for="bookerName">预订人姓名</label>
                        <input type="text" id="bookerName" placeholder="请输入预订人姓名">
                    </div>
                    
                    <div class="form-group">
                        <label for="roomName">会议室名称</label>
                        <input type="text" id="roomName" placeholder="请输入会议室名称">
                    </div>
                    
                    <div class="form-group">
                        <label for="startDate">开始时间</label>
                        <input type="datetime-local" id="startDate">
                    </div>
                    
                    <div class="form-group">
                        <label for="endDate">结束时间</label>
                        <input type="datetime-local" id="endDate">
                    </div>
                </div>
                
                <div class="form-buttons">
                    <button type="button" class="btn reset-btn" onclick="resetForm()">
                        <i class="fas fa-redo"></i> 重置
                    </button>
                    <button type="submit" class="btn search-btn">
                        <i class="fas fa-search"></i> 查询
                    </button>
                </div>
            </form>
        </div>
        
        <div class="results-section">
            <h2 class="section-title"><i class="fas fa-list"></i> 查询结果</h2>
            <div class="table-container">
                <table id="resultsTable">
                    <thead>
                        <tr>
                            <th>会议ID</th>
                            <th>会议室名称</th>
                            <th>预订人</th>
                            <th>开始时间</th>
                            <th>结束时间</th>
                            <th>会议主题</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="resultsTableBody">
                        <tr>
                            <td colspan="8" class="no-results">请输入查询条件并点击查询按钮</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div id="pagination" class="pagination" style="display: none;">
                <button id="firstPage">首页</button>
                <button id="prevPage">上一页</button>
                <span id="pageInfo" class="current-page">第 1 页</span>
                <button id="nextPage">下一页</button>
                <button id="lastPage">末页</button>
            </div>
            <div id="paginationInfo" class="pagination-info" style="display: none;"></div>
        </div>
    </div>
    
    <!-- 更新会议弹窗 -->
    <div id="updateModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeUpdateModal()">&times;</span>
            <div class="modal-header">
                <h2><i class="fas fa-edit"></i> 更新会议信息</h2>
            </div>
            <div class="modal-body">
                <form id="updateForm">
                    <input type="hidden" id="updateMeetingId">
                    <div class="form-row">
                        <label for="updateMeetingTopic">会议主题</label>
                        <input type="text" id="updateMeetingTopic" required>
                    </div>
                    <div class="form-row">
                        <label for="updateCancelFlag">会议状态</label>
                        <select id="updateCancelFlag">
                            <option value="0">正常</option>
                            <option value="1">已取消</option>
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="modal-btn cancel-btn" onclick="closeUpdateModal()">
                    <i class="fas fa-times"></i> 取消
                </button>
                <button class="modal-btn save-btn" onclick="saveMeetingUpdate()">
                    <i class="fas fa-save"></i> 保存
                </button>
            </div>
        </div>
    </div>
    
    <!-- 通知 -->
    <div id="notification" class="notification"></div>
    
    <script>
        // 全局变量用于分页
        let currentPage = 1;
        const pageSize = 5; // 每页显示5条记录
        let allResults = []; // 存储所有查询结果
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 表单提交事件
            document.getElementById('searchForm').addEventListener('submit', function(e) {
                e.preventDefault();
                currentPage = 1; // 重置到第一页
                searchMeetings();
            });
            
            // 分页按钮事件
            document.getElementById('firstPage').addEventListener('click', goToFirstPage);
            document.getElementById('prevPage').addEventListener('click', goToPrevPage);
            document.getElementById('nextPage').addEventListener('click', goToNextPage);
            document.getElementById('lastPage').addEventListener('click', goToLastPage);
            
            // 点击模态框外部关闭模态框
            window.addEventListener('click', function(event) {
                const modal = document.getElementById('updateModal');
                if (event.target === modal) {
                    closeUpdateModal();
                }
            });
        });
        
        // 查询会议
        function searchMeetings() {
            const bookerName = document.getElementById('bookerName').value.trim();
            const roomName = document.getElementById('roomName').value.trim();
            const startDate = document.getElementById('startDate').value;
            const endDate = document.getElementById('endDate').value;
            
            // 构建查询参数
            let params = new URLSearchParams();
            if (bookerName) params.append('bookerName', bookerName);
            if (roomName) params.append('roomName', roomName);
            if (startDate) {
                // 确保日期时间格式包含秒
                let startDateTime = startDate;
                if (startDateTime.length === 16) { // 格式为 yyyy-MM-ddTHH:mm
                    startDateTime += ':00'; // 添加 :00 秒
                }
                params.append('startDate', startDateTime);
            }
            if (endDate) {
                // 确保日期时间格式包含秒
                let endDateTime = endDate;
                if (endDateTime.length === 16) { // 格式为 yyyy-MM-ddTHH:mm
                    endDateTime += ':00'; // 添加 :00 秒
                }
                params.append('endDate', endDateTime);
            }
            
            // 如果没有查询条件，提示用户
            if (!bookerName && !roomName && !startDate && !endDate) {
                showNotification('请至少输入一个查询条件', 'error');
                return;
            }
            
            // 发送查询请求到新的API接口
            fetch(`/api/meetings/bookings?${params.toString()}`)
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        allResults = data.data; // 直接使用返回的真实数据
                        displayResults(allResults);
                    } else {
                        allResults = [];
                        displayResults([]);
                        showNotification(data.msg || '查询失败', 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showNotification('查询时发生错误', 'error');
                    allResults = [];
                    displayResults([]);
                });
        }
        
        // 显示查询结果
        function displayResults(meetings) {
            const tbody = document.getElementById('resultsTableBody');
            tbody.innerHTML = '';
            
            if (meetings && meetings.length > 0) {
                // 计算分页信息
                const startIndex = (currentPage - 1) * pageSize;
                const endIndex = Math.min(startIndex + pageSize, meetings.length);
                const pageResults = meetings.slice(startIndex, endIndex);
                
                pageResults.forEach(meeting => {
                    const row = document.createElement('tr');
                    // 处理时间格式
                    let startTime = '-';
                    let endTime = '-';
                    
                    if (meeting.startTime) {
                        // 解析后端返回的时间字符串
                        const startDateTime = new Date(meeting.startTime);
                        if (!isNaN(startDateTime.getTime())) {
                            startTime = startDateTime.toLocaleString('zh-CN', {
                                year: 'numeric',
                                month: '2-digit',
                                day: '2-digit',
                                hour: '2-digit',
                                minute: '2-digit',
                                second: '2-digit'
                            });
                        }
                    }
                    
                    if (meeting.endTime) {
                        // 解析后端返回的时间字符串
                        const endDateTime = new Date(meeting.endTime);
                        if (!isNaN(endDateTime.getTime())) {
                            endTime = endDateTime.toLocaleString('zh-CN', {
                                year: 'numeric',
                                month: '2-digit',
                                day: '2-digit',
                                hour: '2-digit',
                                minute: '2-digit',
                                second: '2-digit'
                            });
                        }
                    }
                    
                    const status = meeting.cancelFlag === 1 ? '已取消' : '正常';
                    
                    row.innerHTML = `
                        <td>${meeting.id || '-'}</td>
                        <td>${meeting.roomName || '-'}</td>
                        <td>${meeting.bookerName || '-'}</td>
                        <td>${startTime}</td>
                        <td>${endTime}</td>
                        <td>${meeting.meetingTopic || '-'}</td>
                        <td>${status}</td>
                        <td>
                            <div class="action-buttons">
                                <button onclick="updateMeeting(${meeting.id})" class="action-btn update-btn">
                                    <i class="fas fa-edit"></i> 更新
                                </button>
                                <button onclick="deleteMeeting(${meeting.id})" class="action-btn delete-btn">
                                    <i class="fas fa-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    `;
                    tbody.appendChild(row);
                });
                
                // 显示分页控件
                if (meetings.length > pageSize) {
                    document.getElementById('pagination').style.display = 'flex';
                    document.getElementById('paginationInfo').style.display = 'block';
                    updatePaginationInfo(meetings.length);
                } else {
                    document.getElementById('pagination').style.display = 'none';
                    document.getElementById('paginationInfo').style.display = 'none';
                }
            } else {
                const row = document.createElement('tr');
                row.innerHTML = `<td colspan="8" class="no-results">未找到符合条件的会议记录</td>`;
                tbody.appendChild(row);
                document.getElementById('pagination').style.display = 'none';
                document.getElementById('paginationInfo').style.display = 'none';
            }
        }
        
        // 更新分页信息
        function updatePaginationInfo(totalCount) {
            const totalPages = Math.ceil(totalCount / pageSize);
            document.getElementById('pageInfo').textContent = `第 ${currentPage} 页，共 ${totalPages} 页`;
            document.getElementById('paginationInfo').textContent = `共 ${totalCount} 条记录，当前显示 ${Math.min((currentPage - 1) * pageSize + 1, totalCount)} - ${Math.min(currentPage * pageSize, totalCount)} 条`;
            
            // 更新按钮状态
            const totalPagesCount = Math.ceil(totalCount / pageSize);
            document.getElementById('firstPage').disabled = currentPage === 1;
            document.getElementById('prevPage').disabled = currentPage === 1;
            document.getElementById('nextPage').disabled = currentPage === totalPagesCount;
            document.getElementById('lastPage').disabled = currentPage === totalPagesCount;
        }
        
        // 分页导航函数
        function goToFirstPage() {
            currentPage = 1;
            displayResults(allResults);
        }
        
        function goToPrevPage() {
            if (currentPage > 1) {
                currentPage--;
                displayResults(allResults);
            }
        }
        
        function goToNextPage() {
            const totalPages = Math.ceil(allResults.length / pageSize);
            if (currentPage < totalPages) {
                currentPage++;
                displayResults(allResults);
            }
        }
        
        function goToLastPage() {
            currentPage = Math.ceil(allResults.length / pageSize);
            displayResults(allResults);
        }
        
        // 更新会议 - 打开弹窗
        function updateMeeting(meetingId) {
            // 查找要更新的会议信息
            const meeting = allResults.find(m => m.id === meetingId);
            if (meeting) {
                // 填充表单数据
                document.getElementById('updateMeetingId').value = meeting.id;
                document.getElementById('updateMeetingTopic').value = meeting.meetingTopic || '';
                document.getElementById('updateCancelFlag').value = meeting.cancelFlag || '0';
                
                // 显示弹窗
                document.getElementById('updateModal').style.display = 'flex';
            } else {
                showNotification('未找到会议信息', 'error');
            }
        }
        
        // 关闭更新弹窗
        function closeUpdateModal() {
            document.getElementById('updateModal').style.display = 'none';
        }
        
        // 保存会议更新
        function saveMeetingUpdate() {
            const meetingId = document.getElementById('updateMeetingId').value;
            const meetingTopic = document.getElementById('updateMeetingTopic').value.trim();
            const cancelFlag = document.getElementById('updateCancelFlag').value;
            
            // 验证输入
            if (!meetingTopic) {
                showNotification('请输入会议主题', 'error');
                return;
            }
            
            // 准备更新数据
            const updateData = {
                meetingTopic: meetingTopic,
                cancelFlag: parseInt(cancelFlag)
            };
            
            // 发送更新请求
            fetch(`/api/meetings/bookings/${meetingId}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(updateData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    showNotification('更新成功', 'success');
                    closeUpdateModal();
                    // 重新查询数据以刷新表格
                    searchMeetings();
                } else {
                    showNotification(data.msg || '更新失败', 'error');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showNotification('更新时发生错误', 'error');
            });
        }
        
        // 删除会议
        function deleteMeeting(meetingId) {
            if (confirm(`确定要删除会议ID为 ${meetingId} 的记录吗？`)) {
                // 发送删除请求
                fetch(`/api/meetings/bookings/${meetingId}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        showNotification('删除成功', 'success');
                        // 重新查询数据以刷新表格
                        searchMeetings();
                    } else {
                        showNotification(data.msg || '删除失败', 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showNotification('删除时发生错误', 'error');
                });
            }
        }
        
        // 重置表单
        function resetForm() {
            document.getElementById('searchForm').reset();
            document.getElementById('resultsTableBody').innerHTML = '<tr><td colspan="8" class="no-results">请输入查询条件并点击查询按钮</td></tr>';
            document.getElementById('pagination').style.display = 'none';
            document.getElementById('paginationInfo').style.display = 'none';
            allResults = [];
            currentPage = 1;
        }
        
        // 显示通知
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.className = 'notification ' + type;
            notification.style.display = 'block';
            
            setTimeout(() => {
                notification.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html>